#include <stdio.h>
#include "sum.h"
#include "config_1.h"

int main(int argc,char**argv)
{
    if(argc != 2){
        error(1,0,"lack of server_conf");
    }
    HashTable ht;
    initHashTable(&ht);
    readConfig(argv[1],&ht);

    int err;
    //-----------------------------------------------------------------------//
    //建立网络连接
    //建立一个连接用户的listenfd,建立一个用于文件传输的tsmfd
    printf("[system]building network config...\n");
    socklen_t len = sizeof(struct sockaddr); 
    //printf("%s\n", (char*)find(&ht, PORT));
    //printf("%s\n", (char*)find(&ht, TSM_PORT));
    struct sockaddr* addr = addr_create(find(&ht,IP),atoi((char*)find(&ht,PORT)));
    struct sockaddr* tsmaddr = addr_create(find(&ht,IP),atoi((char*)find(&ht,TSM_PORT)));

    int listenfd = socket(AF_INET,SOCK_STREAM,0);



    if(listenfd == -1){
        error(1,errno,"socket");
    }

    int tsmfd = socket(AF_INET,SOCK_STREAM,0);
    if(listenfd == -1){
        error(1,errno,"socket");
    }

    int on = 1;
    err = setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
    if(err == -1){
        error(1,errno,"setsockopt");
    }
    err = setsockopt(tsmfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
    if(err == -1){
        error(1,errno,"setsockopt");
    }

    err = bind(listenfd,addr,len);
    if(err == -1){
        error(1,errno,"bind");
    }
    err = bind(tsmfd,tsmaddr,len);
    if(err == -1){
        error(1,errno,"bind");
    }

    err = listen(listenfd,LISTEN_LEN);
    if(err == -1){
        error(1,errno,"listen");
    }
    err = listen(tsmfd,LISTEN_LEN);
    if(err == -1){
        error(1,errno,"listen");
    }
    //连接数据库
    MYSQL* p_mysql;
    p_mysql = mysql_disk_connect(MYSQL_IP,MYSQL_PORT,MYSQL_NAME,MYSQL_PASSWD,MYSQL_DB);
    //-----------------------------------------------------------------------//
    //
    //创建文件传输线程池
    printf("[system]building user transmit pthreadpool...\n");
    threadpool* pth_tsm = threadpool_init(4);
    cmd_pool_tast input;
    input.q = pth_tsm->q;
    input.tsm_fd = tsmfd;
    input.p_mysql = p_mysql;
    threadpool_start(pth_tsm,threadpool_tsm_function,(void*)&input);


    //创建解析命令线程池
    printf("[system]building user cmd pthreadpool...\n");
    threadpool* pth_cmd = threadpool_init(4);
    threadpool_start(pth_cmd,threadpool_cmd_function,(void*)pth_cmd->q);

    //创建超时断联线程池
    ut_init();


    threadpool* pth_del = threadpool_init(1);
    threadpool_start(pth_del,threadpool_del_function,(void*)pth_del->q);
    
    //-----------------------------------------------------------------------//
    //设置监听
    int epfd = epoll_create1(0);
    epoll_mod(epfd,EPOLL_CTL_ADD,EPOLLIN,listenfd);
    struct epoll_event recv_events[RECV_EVENTS_NUM];


    
    int recv_num;

    //开始监听
    printf("[system]the server is ready...\n");
    while(1){
        recv_num = epoll_wait(epfd,recv_events,RECV_EVENTS_NUM,-1);
        for(int i = 0;i<recv_num;i++){
            if(recv_events[i].data.fd == listenfd){
                //进行新用户的接收
                server_user_recv(listenfd,epfd,p_mysql);
            }else{
                //进行命令解析
                server_msg_recv(recv_events[i].data.fd,epfd,pth_cmd,pth_tsm);
            }
        }
    }
}

//-----------------------------------------------------------------------//
